import hashlib  
import os

def GetFileHashCode(filename):
	ret = os.path.exists(filename)
	if not ret:
		return ""
	f = open(filename, "r", encoding="utf-8")
	s = f.read()
	encoded_s = s.encode('utf-8')  
	hash_object = hashlib.sha256(encoded_s)  
	current_hash = hash_object.hexdigest() 
	return current_hash

def GetLastFileHashCode(filename):
	hash_list = {} 
	ret = os.path.exists(filename)
	if not ret:
		return hash_list

	f = open(filename, "r", encoding="utf-8")
	line = " "
	while line:
		line = f.readline()
		if len(line) > 0:
			ind = line.find(" ")
			str_filename = line[:ind].strip()
			str_hashcode = line[ind:].strip()
			hash_list[str_filename] = str_hashcode
	return hash_list

def FileNeedToGenerate(filename, hash_code_origin):
	hash_code_curr = GetFileHashCode(filename)
	if hash_code_curr == hash_code_origin:
		return False
	else:
		return True

def FileSaveHashList(filename, hash_list):
	file = open(filename, "w", encoding="utf-8")
	for key, value in hash_list.items():
		line = f"{key} {value}\n"
		file.write(line)
	file.close()

#a = GetLastFileHashCode("/data/temp/1.txt")
#print(a)
# a = GetFileHashCode("/data/temp/1.txt")
# print(a)

#FileSaveHashList("/data/temp/hash.txt", a)